import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import java.util.ArrayList;
import java.util.Calendar;

public class Logic implements ActionListener{

	public static void main(String[] args){
		Logic launch = new Logic();
	}

	// STATES
	public static final int STARTUP = 0;
	public static final int LOGIN = 1;
	public static final int WELCOME = 2;
	public static final int QUESTION = 3;
	
	// GUI MEMBERS
	GUI mainGUI;
	
	// TIME MEMBERS
	Calendar calendar = Calendar.getInstance();
	long currentTime;
	long startTime;

	// DECKS AND QUESTIONS
	public Node loadedDeck;
	public Node currentQuestion;
	public int questionNumber = 0;
	
	// MAIN DATA
	public Node MasterNode;
	public ArrayList<Node> Users = new ArrayList<Node>();
	public ArrayList<Node> Decks = new ArrayList<Node>();
	
	public int currentState;
	
	public Logic(){
		MasterNode = parse("MASTER.xml");
		Users = MasterNode.getAllofType("User");
		Decks = MasterNode.getAllofType("Deck");
		loadDeck(Decks.get(0).getAttribute("file"));
		currentTime = calendar.getTimeInMillis();
		mainGUI = new GUI(this);
	}
	
	public Node parse(String file){
		Parser parser = new Parser();
		return parser.parse(file);
	}
	
	public void loadDeck(String file){
		loadedDeck = parse(file);
	}
	
	public void loadNextQuestion(){
		questionNumber = (questionNumber+1)%loadedDeck.nbChildren();
		mainGUI.loadQuestion(loadedDeck.getChild(questionNumber));
	}
	
	public void actionPerformed(ActionEvent e) {
		String temp = e.getActionCommand();
		if(temp.equals("menuLogin"))
			switchState(LOGIN);
		if(temp.equals("menuLogout")){	}
		if(temp.equals("menuCreateUser")){	}
		if(temp.equals("menuLoadDeck")){	}
		if(temp.equals("Enter")){	}
		if(temp.equals("Back"))
			switchState(QUESTION);
		if(e.getSource() instanceof JRadioButton)
			switchState(QUESTION);
		if(temp.equals("")){	}
		if(temp.equals("")){	}
	}
	
	public void switchState(int state){
		currentTime = calendar.getTimeInMillis();
		if(state == LOGIN)
			mainGUI.switchPane(mainGUI.loginPanel);
		if(state == QUESTION){
			mainGUI.switchPane(mainGUI.questionPanel); 
			loadNextQuestion();			
		}
		currentState = state;
	}
	
	
	
}
